package edu.utd.text.game.domain;

import java.io.Serializable;
import java.util.ArrayList;

import edu.utd.text.game.domain.areas.Room;
import edu.utd.text.game.domain.exceptions.DuplicateRoomException;

/**
 * GameMap.
 * This is a data object that contains all the
 * rooms in the game.  It is used for saving 
 * and loading the game conveniently.
 * 
 * Rooms are stored in the rooms HashMap, indexed
 * by their String room name.
 * 
 * @author troy
 *
 */

public class GameMap implements Serializable {	
	private ArrayList<Room> rooms;
	private Room mainRoom;
	
	public GameMap(ArrayList<Room> rooms) {
		this.rooms = rooms;
	}
	
	public GameMap() {
		rooms = new ArrayList<Room>();
	}
	
	public void addRoom(Room room) throws DuplicateRoomException {
		if (this.containsRoom(room)) {
			throw new DuplicateRoomException();
		}
		rooms.add(room);
	}
	
	public boolean removeRoom(Room room) {
		return rooms.remove(room);
	}
	
	//TODO: TEST ME!
	public boolean removeRoomByName(String roomToRemove) {
		for (Room aRoom : rooms) {
			if (aRoom.getRoomName().equals(roomToRemove)) {
				return rooms.remove(aRoom);
			}
		}
		return false;
	}
	
	public boolean containsRoom(Room otherRoom) {
		for (Room room : rooms) {
			if (room.equals(otherRoom)) {
				return true;
			}
		}
		return false;
	}
	
	//TODO: TEST ME!
	public boolean containsRoomByName(String roomName) {
		for (Room aRoom : rooms) {
			if (aRoom.getRoomName().equals(roomName)) {
				return true;
			}
		}
		return false;
	}
	
	  /////////////
	 // GETTERS //
	/////////////
	
	public ArrayList<Room> getRooms() {
		return rooms;
	}
	
	//TODO: TEST ME!
	public Room getRoomByName(String roomName) {
		for (Room room : rooms) {
			if (room.getRoomName().equals(roomName)) {
				return room;
			}
		}
		return null;
	}
	
	public Room getMainRoom() {
		if (mainRoom == null) {
			//if no main room is set, use the one at index 0.
			//NOTE: still may return null if no rooms are added.
			if (!getRooms().isEmpty()) {
				mainRoom = getRooms().get(0);
			}
		}
		return mainRoom;
	}
	
	  /////////////
	 // SETTERS //
	/////////////
	
	public void setMainRoom(Room mainRoom) {
		this.mainRoom = mainRoom;
	}

}
